import { createRouter, createWebHistory } from 'vue-router'
import StoriesView from '../views/StoriesView.vue'
import { userStore } from '../stores/user'

// 路由守卫，检查管理员权限
const adminGuard = (to, from, next) => {
  const state = userStore.getState()
  if (state.isAuthenticated && state.currentUser && state.currentUser.isAdmin) {
    next()
  } else {
    next('/admin/login')
  }
}

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('../views/HomeView.vue')
    },
    {
      path: '/mbti-test',
      name: 'mbti-test',
      component: () => import('../views/MBTITestView.vue')
    },
    {
      path: '/questions',
      name: 'questions',
      component: () => import('../views/QuestionsView.vue')
    },
    {
      path: '/ai-analysis',
      name: 'ai-analysis',
      component: () => import('../views/AIAnalysisView.vue')
    },
    {
      path: '/advice/:id',
      name: 'advice',
      component: () => import('../views/AdviceView.vue')
    },
    {
      path: '/advice-detail/:id',
      name: 'advice-detail',
      component: () => import('../views/AdviceDetailView.vue')
    },
    {
      path: '/share',
      name: 'share',
      component: () => import('../views/ShareView.vue')
    },
    {
      path: '/stories',
      name: 'stories',
      component: StoriesView
    },
    {
      path: '/story/:id',
      name: 'story-detail',
      component: () => import('../views/StoryDetailView.vue')
    },
    // 管理员相关路由
    {
      path: '/admin/login',
      name: 'admin-login',
      component: () => import('../views/AdminLoginView.vue')
    },
    {
      path: '/admin/dashboard',
      name: 'admin-dashboard',
      component: () => import('../views/admin/DashboardView.vue'),
      beforeEnter: adminGuard
    },
    {
      path: '/admin/users',
      name: 'admin-users',
      component: () => import('../views/admin/UsersView.vue'),
      beforeEnter: adminGuard
    },
    {
      path: '/admin/questions',
      name: 'admin-questions',
      component: () => import('../views/admin/QuestionsView.vue'),
      beforeEnter: adminGuard
    },
    {
      path: '/admin/stories',
      name: 'admin-stories',
      component: () => import('../views/admin/StoriesView.vue'),
      beforeEnter: adminGuard
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('../views/AboutView.vue')
    },
    {
      path: '/join-us',
      name: 'join-us',
      component: () => import('../views/JoinUsView.vue')
    },
    {
      path: '/position/:id',
      name: 'position-detail',
      component: () => import('../views/PositionDetailView.vue')
    },
    {
      path: '/auth',
      name: 'auth',
      component: () => import('../views/AuthView.vue')
    },
    {
      path: '/member',
      name: 'member',
      component: () => import('../views/MemberView.vue')
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'not-found',
      component: () => import('../views/NotFoundView.vue')
    }
  ]
})

export default router